var map;
var url = document.URL;
var count = 1;
var lat;
var lon;

function initialize() {
	//get values from application
	 var latlng = new google.maps.LatLng(43.845794,18.360558);
	 
	 var myOptions = {
	      zoom: 8,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    
	    map = new google.maps.Map(document.getElementById("map"),
	        myOptions);
	    
	    google.maps.event.addListener(map, 'click', function(event) {
	    	 placeMarker(event.latLng);
	    });
};
 
 function placeMarker(location) {
	  lat = location.lat();
	  lon = location.lng();

	  var marker = new google.maps.Marker({
	      position: location, 
	      map: map,
	      draggable: true,
	      animation: google.maps.Animation.DROP
	  });
 };
 
 function addProperty() {
	 count++; 

	 $.getJSON('gettypes', function(data) {
		  var name = '</tr><tr><td><input type="text" name="name'+count+'" /></td>';
		  var options='';
		  
		  $.each(data, function(key, val) {
			  options +='<option value="' + key + '">' + val + '</option>';
		  });
		  
		  var type = '<td><select name="name'+count+'">' + options + '</select></td></tr>';
			 
		  $("#typeTable").append(name+type);

	});
	 
 };
 
 function savePoi() {
	 var params = [
	               {
	                 name: "lat",
	                 value: lat
	               },
	               {
	                  name: "lon",
	                  value: lon
	               }
	             ];

	 $('#poiform').submit(function(){ //listen for submit event
		    $.each(params, function(i,param){
		        $('<input />').attr('type', 'hidden')
		            .attr('name', param.name)
		            .attr('value', param.value)
		            .appendTo('#poiform');
		    });

		    return true;
		});	

	 };

 function getPois(){
	//send query for database
	 
	//return pois, make markers from them
 }	 
 
 function getTemplate() {
	 //load html form with temaplate id
	 var e = document.getElementById("templates");
	 var tempValue = e.options[e.selectedIndex].value;
	 $('#create').load('getpoitemp',{"id": tempValue});
 };
 
function readTemplate(checkbox){
	if (checkbox.checked){
		$('#templateprop').load('templatequery',{"id": checkbox.value});		
	}

 };
